package com.yiqi.liebang.feature.mine.view.adapter;

import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.framework.api.UserApi;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class FriendsAdapter extends BaseQuickAdapter<UserCenterBo.UserFriendBean, BaseViewHolder> {

    Callback.CommonCallback commonCallback;

    public FriendsAdapter() {
        super(R.layout.item_userinfo_friend);
    }

    @Override
    protected void convert(BaseViewHolder helper, UserCenterBo.UserFriendBean item) {
        AM.image().bindToCircleObject(item.getUserHead(), helper.getView(R.id.iv_friend_head));
        helper.setText(R.id.tv_visitor_name, item.getUserName())
                .setText(R.id.tv_visitor_position,  (TextUtils.isEmpty(item.getPosition()) ? "" : item.getPosition()))
                .setVisible(R.id.iv_user_position, item.getIsOccupation() == 1 ? true : false)
                .setVisible(R.id.iv_user_certification, item.getIsBasic() == 1 ? true : false)
                .setBackgroundRes(R.id.btn_friends_add, item.getIsApplyStatus() == 0 ? R.drawable.bg_friends_ysq : R.drawable.bg_edit_search)
                .setTextColor(R.id.btn_friends_add, item.getIsApplyStatus() == 0 ? AM.app().getResources().getColor(R.color.text_999999) : AM.app().getResources().getColor(R.color.primary_highlight))
                .setText(R.id.btn_friends_add, item.getIsApplyStatus() == 0 ? "已申请" : "+好友")
                .addOnClickListener(R.id.btn_friends_add);

        helper.getView(R.id.btn_friends_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PeopleBo peopleBo = new PeopleBo();
                peopleBo.setApplicationUserUid(item.getUserUid());

                AM.api().createApiService(UserApi.class)
                        .getAddFriend(peopleBo)
                        .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                commonCallback.onSubscribe(d);
                            }

                            @Override
                            public void onNext(String s) {
                                commonCallback.onSuccess(s);
                                helper.setTextColor(R.id.btn_friends_add, AM.res().getColor(R.color.text_999999))
                                        .setBackgroundRes(R.id.btn_friends_add, R.drawable.bg_friends_ysq)
                                        .setText(R.id.btn_friends_add, "已申请");
                            }

                            @Override
                            public void onError(Throwable e) {
                                commonCallback.onError(e);
                                T.showShort(e.getMessage());
                            }

                            @Override
                            public void onComplete() {
                                commonCallback.onComplete();
                            }
                        });
            }

        });

    }

    public void setCommonCallback(Callback.CommonCallback commonCallback) {
        this.commonCallback = commonCallback;
    }
}
